package com.micookie.v2ex.model.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;
import com.micookie.v2ex.api.APIConfig;

/**
 * Created by Administrator on 2016-10-25.
 */

public class Topic implements Parcelable {

    /**
     * id : 315934
     * title : 杭州有哪些值得推荐的互联网行业猎头？
     * url : http://www.v2ex.com/t/315934
     * content : 麻烦大家推荐一下自己找工作时接触过的靠谱猎头，希望是比较中端的那种，自己找工作想走猎头。
     * content_rendered : <p>麻烦大家推荐一下自己找工作时接触过的靠谱猎头，希望是比较中端的那种，自己找工作想走猎头。</p>
     * <p>
     * replies : 0
     * member : {"id":159197,"username":"invaliduser","tagline":"","avatar_mini":"//cdn.v2ex.co/gravatar/e118e91fb4d7cad8e23c533f934711a0?s=24&d=retro","avatar_normal":"//cdn.v2ex.co/gravatar/e118e91fb4d7cad8e23c533f934711a0?s=48&d=retro","avatar_large":"//cdn.v2ex.co/gravatar/e118e91fb4d7cad8e23c533f934711a0?s=73&d=retro"}
     * node : {"id":300,"name":"programmer","title":"程序员","title_alternative":"Programmer","url":"http://www.v2ex.com/go/programmer","topics":13661,"avatar_mini":"//cdn.v2ex.co/navatar/94f6/d7e0/300_mini.png?m=1477535591","avatar_normal":"//cdn.v2ex.co/navatar/94f6/d7e0/300_normal.png?m=1477535591","avatar_large":"//cdn.v2ex.co/navatar/94f6/d7e0/300_large.png?m=1477535591"}
     * created : 1477561198
     * last_modified : 1477561268
     * last_touched : 1477561018
     */

    private int id;
    private String title;
    private String url;
    private String content;
    @SerializedName("content_rendered")
    private String contentRendered;
    private int replies; //回复数
    private int clicks; //点击数
    private Member member;
    private Node node;
    private int created;
    private String createdFormat;
    private String lastReply;
    @SerializedName("last_modified")
    private int lastModified;
    @SerializedName("last_touched")
    private int lastTouched;


    public Topic() {

    }


    public static final Creator<Topic> CREATOR = new Creator<Topic>() {
        @Override
        public Topic createFromParcel(Parcel in) {
            return new Topic(in);
        }

        @Override
        public Topic[] newArray(int size) {
            return new Topic[size];
        }
    };

    public int getId() {
        return id;
    }

    public Topic setId(int id) {
        this.id = id;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public Topic setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getUrl() {
        return url;
    }

    /**
     * 一条主题，每100条评论分一页
     * 获取第一页评论的URL
     *
     * @return
     */
    public String getUrlFirstPage() {
        return url.replaceAll("#reply\\d+", "?p=1");
    }

    public Topic setUrl(String url) {
        if (url.startsWith("/t/")) {
            url = APIConfig.getBaseUrl() + url;
        }
        this.url = url;
        return this;
    }

    public String getContent() {
        if (content == null || content.isEmpty())
            return "";
        //TODO 此处是markdown等内容的css布局。需要替换为本地文件，节省流量
        String css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\"  href=\"https://www.v2ex.com/static/css/style.css\" />";
        content = css + content;
        return content;
    }

    public Topic setContent(String content) {
        this.content = content;
        return this;
    }

    public String getContentRendered() {
        return contentRendered;
    }

    public Topic setContentRendered(String contentRendered) {
        this.contentRendered = contentRendered;
        return this;
    }

    public int getReplies() {
        return replies;
    }

    public Topic setReplies(int replies) {
        this.replies = replies;
        return this;
    }

    public Member getMember() {
        return null == member ? member = new Member() : member;
    }

    public Topic setMember(Member member) {
        this.member = member;
        return this;
    }

    public Node getNode() {
        return null == node ? node = new Node() : node;
    }

    public Topic setNode(Node node) {
        this.node = node;
        return this;
    }

    public int getCreated() {
        return created;
    }

    public Topic setCreated(int created) {
        this.created = created;
        return this;
    }

    public String getCreatedFormat() {
        return createdFormat;
    }

    public Topic setCreatedFormat(String createdFormat) {
        this.createdFormat = createdFormat;
        return this;
    }


    public String getLastReply() {
        return lastReply;
    }

    public Topic setLastReply(String lastReply) {
        this.lastReply = lastReply;
        return this;
    }


    public int getClicks() {
        return clicks;
    }

    public void setClicks(int clicks) {
        this.clicks = clicks;
    }

    public int getLastModified() {
        return lastModified;
    }

    public Topic setLastModified(int lastModified) {
        this.lastModified = lastModified;
        return this;
    }

    public int getLastTouched() {
        return lastTouched;
    }

    public Topic setLastTouched(int lastTouched) {
        this.lastTouched = lastTouched;
        return this;
    }

    public String getOtherInfo() {
        if (getCreatedFormat().trim().isEmpty()) {
            return "";
        }
        StringBuffer stringBuff = new StringBuffer();
//        stringBuff.append(" • ");
//        stringBuff.append(getMember().getUsername());
        stringBuff.append("• ");
        stringBuff.append(getCreatedFormat());
//        stringBuff.append(" • 最后回复来自 ");
//        stringBuff.append(getLastReply());
        return stringBuff.toString();
    }

    protected Topic(Parcel in) {
        id = in.readInt();
        title = in.readString();
        url = in.readString();
        content = in.readString();
        contentRendered = in.readString();
        replies = in.readInt();
        clicks = in.readInt();
        member = in.readParcelable(Member.class.getClassLoader());
        created = in.readInt();
        createdFormat = in.readString();
        lastReply = in.readString();
        lastModified = in.readInt();
        lastTouched = in.readInt();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(id);
        parcel.writeString(title);
        parcel.writeString(url);
        parcel.writeString(content);
        parcel.writeString(contentRendered);
        parcel.writeInt(replies);
        parcel.writeInt(clicks);
        parcel.writeParcelable(member, i);
        parcel.writeInt(created);
        parcel.writeString(createdFormat);
        parcel.writeString(lastReply);
        parcel.writeInt(lastModified);
        parcel.writeInt(lastTouched);
    }
}
